#!/bin/bash -e

# snp for parallel

function info() {
echo Usage: `basename $0` '[-lnsgGacr] reads1.fq.gz reads2.fq.gz'
exit 65
}

while getopts  ":l:p:f:n:s:gGa:cr:" opts; do
	case  $opts  in
	a) cut=$OPTARG;;
	c) cnv=T;;
	l) interval=$OPTARG;;
	n) nums=$OPTARG;;
	s) sample_name=$OPTARG;;
	g) single_cell=T;;
	G) somatic=T;;
	p) out_prefix=$OPTARG;;
	f) suffix=$OPTARG;;
	r) fq_rows=$OPTARG;;
	*) info;;
	esac
done
shift $(($OPTIND - 1))


if [ $# -lt 2 ]; then info; fi

r1=`rel_abs.sh $1`
r2=`rel_abs.sh $2` # must run before mkdir=true

makedir=true

if test -z "$interval"; then interval=$data_path/intervals/1/b37.bed; fi


. $var

IFS=$'\n'

split_fastq=split_fastq.sh

if test -z "$cut"; then
cutadp=cutadp.sh
elif test "$cut" = "general"; then
cutadp=cutadp.sh
elif test "$cut" = "malbac"; then
cutadp=cutadp_malbac.sh
elif test "$cut" = "ctc80"; then
cutadp=cutadp_ctc80_discard_trimmed.sh
fi

aln=aln_bwa_bt.sh
sam2b=sam2b_samtools.sh
merge=merge_bam.sh
hmmcopy=hmmcopyb38.sh
bed_split=bed-split.py
split_bam=split_bam_0701.sh
extract_bam=extract_bam_bed.sh
extract_bam_bed=extract_bam_bed.sh
markdup=deldup.sh
realn=realign_bt.sh
recal=recal0630.sh
ug=ug.sh
annos=annos0711.sh
combine_vcfs=combine_vcfs.sh
impact=impact.sh
format_simple=format_simple_0629.sh

pid=.pid.log

# using sub for identifying, modified no need
# output like $out_prefix.1.0000.fastq

# splitting should has suffix fastq fq 
# pigz -cdf -p2 $1|split_fastq.sh -l6000000 -p$out_prefix.1.split - &
# pigz -cdf -p2 $2|split_fastq.sh -l6000000 -p$out_prefix.2.split -

# wait

if test -z "$fq_rows"; then fq_rows=4000000; fi

sub -F -nsplit -m4 -p2 "pigz -cdf -p2 $r1|split_fastq.sh -l$fq_rows -p$out_prefix.1.split -"
sub -w5 -F -nsplit -m4 -p2 "pigz -cdf -p2 $r2|split_fastq.sh -l$fq_rows -p$out_prefix.2.split -"

while true; do

	if test 2 -eq `cat $pid|grep -c "$split_fastq - done"`; then #must 2 done find
		break
	else
		sleep 10
	fi
done


:<<split1

sub -F -nsplit -m4 -p4 "pigz -cdf -p2 $1|split_fastq.sh -l6000000 -p$out_prefix.1.split -"
sub -F -nsplit -m4 -p4 "pigz -cdf -p2 $2|split_fastq.sh -l6000000 -p$out_prefix.2.split -"

while true; do

	if cat .pid.log|grep "$split_fastq -" > /dev/null; then
		break
	else
		sleep 10
	fi

	for item in `ls|perl -ne 'if(/\.(\d\d\d\d$)/){if ($1 - 1 >= 0){print $1-1 . "\n"}}'|sort |uniq -d|perl -ne 'printf "%04d\n", $_'`; do if cat .pid.log|grep "$split_fastq \.$item done" >/dev/null; then echo $split_fastq .$item done; else echo $split_fastq .$item done>>.pid.log; fi; done

	fi

done

split1

cpu2=2
cpu4=4
cpu8=8
mem4=4
mem8=8
mem16=16
mem32=32



fq_list=$out_prefix.fq.list.txt
cutadp_list=$out_prefix.cutadp.list.txt
sam_list=$out_prefix.sam.list.txt
bam_list=$out_prefix.bam.list.txt
bed_list=$out_prefix.bed.list.txt
merge_list=$out_prefix.merge.list.txt
extract_list=$out_prefix.extract.list.txt
extract1_list=$out_prefix.extract1.list.txt
bam_list_list=$out_prefix.bam.list.list.txt
split_list=$out_prefix.split.list.txt
markdup_list=$out_prefix.markdup.list.txt

i_bed_list=$out_prefix.i.bed.list.txt
l_bed_list=$out_prefix.l.bed.list.txt
realn_list=$out_prefix.realn.list.txt
recal_list=$out_prefix.recal.list.txt
ug_list=$out_prefix.ug.list.txt
high_list=$out_prefix.high.list.txt
low_list=$out_prefix.low.list.txt
hl_list=$out_prefix.hl.list.txt
combine_list=$out_prefix.combine.list.txt
impact_high_list=$out_prefix.impact.high.list.txt
impact_low_list=$out_prefix.impact.low.list.txt
impact_hl_list=$out_prefix.impact.hl.list.txt
end_list=$out_prefix.end.list.txt


# i=0
# for item in $(ls $out_prefix.1.*); do
	# i=$((i+1))
	# rename $item $out_prefix.1.$i.fastq $item 
# done

ls $out_prefix.1.split.* > $out_prefix.1.list.txt
ls $out_prefix.2.split.* > $out_prefix.2.list.txt
paste -d' ' $out_prefix.1.list.txt $out_prefix.2.list.txt >$fq_list && rm $out_prefix.1.list.txt $out_prefix.2.list.txt

# monitor.sh -ncut -c1 -m2 $cutadp $fq_list 

# for file1 in $(ls $out_prefix.1.sub.*.fastq.gz); do
	# file2=`echo $file1|sed s/$out_prefix\.1\.sub/$out_prefix.2.sub/`
	# sub -F -ncutadp -m1 -p1 cutadp.sh $file1 $file2 &
# done

i=0
for item in $(cat $fq_list); do
	i=$((i+1))
	sub -w16 -F -ncutadp.$i -m2 -p1 $cutadp -p$out_prefix.$i $item
	echo $out_prefix.$i.1.cut.fastq $out_prefix.$i.2.cut.fastq >> $cutadp_list
	echo $out_prefix.$i.sam >> $sam_list
	echo $out_prefix.$i.sort.bam >> $bam_list

done



monitors.sh -naln -m6 -c4 "$aln -s$sample_name -t4" $cutadp_list $cutadp $fq_list &

monitors.sh -nsam2b -m4 -c4 "$sam2b -t4" $sam_list $aln $cutadp_list &

# wait not wait, not finishing



monitor0707.sh -nmerg -m20 -c6 "$merge -t8" $bam_list $sam2b $sam_list

merge_in_file=$out_prefix.merge.in.txt
echo $bam_list > $merge_in_file

if test -n "$cnv"; then
monitor0707.sh -ndel -m20 -c2 "$markdup" $out_prefix.merge.bam $merge $merge_in_file # can without using monitor, can using sub directly

merge_bam_list=$out_prefix.merge.bam.list.txt
monitor0707.sh -nhmm -m20 -c2 "$hmmcopy -b37" $out_prefix.deldup.bam $markdup $merge_bam_list
fi

if test -z "$nums"; then nums=120; fi

cat $interval|cut -f1-3 >$out_prefix.bed

$bed_split $out_prefix.bed $nums


# monitor0707.sh -nsplit -m2 -c1 $split_bam "$out_prefix.merge.bam $interval $nums" $merge $merge_in_file &


for i in $(seq $nums); do
	echo $out_prefix.merge.bam $out_prefix.$i.bed >> $extract_list
	echo $bam_list >>$bam_list_list
	echo $out_prefix.$i.bed >> $bed_list
	echo $out_prefix.$out_prefix.$i.bed.bam >> $split_list #note that there are 2 out_prefix
	echo $out_prefix.$i.extract.bam >> $extract1_list
	
	echo $out_prefix.$i.markdup.bam >> $markdup_list
	echo $out_prefix.$i.realn.bam >> $realn_list
	echo $out_prefix.$i.realn.recal.bam >> $recal_list
	echo $out_prefix.$i.snp.vcf $out_prefix.$i.indel.vcf >> $ug_list
	echo $out_prefix.$i.high.vcf >> $high_list
	echo $out_prefix.$i.low.vcf  >> $low_list
done

cat $bed_list|sed 's/^/'$out_prefix'.merge.bam /' > $merge_list


cat $bed_list|sed 's/^/-i/' > $i_bed_list
cat $bed_list|sed 's/^/-l /' > $l_bed_list

monitors.sh -nextra -m2 -c2 $extract_bam $extract_list $merge $bam_list_list &

# monitors.sh -nmark  -m8 -c2 "$markdup -m" $split_list $extract_bam_bed $merge_list &
monitors.sh -nmark  -m8 -c2 "$markdup -m" $extract1_list $extract_bam_bed $merge_list &

monitors.sh -nrealn  -m8 -c4 -i$i_bed_list "$realn -n4" $markdup_list $markdup $extract1_list &

monitors.sh -nrecal  -m8 -c4 -i$l_bed_list "$recal -t4" $realn_list $realn $markdup_list &

monitors.sh -nug -m12 -c6 -i$i_bed_list $ug $recal_list $recal $realn_list &

# monitors.sh -nano -m12 -c6 -l$i_bed_list $annos -gm $ug_list $ug $recal_list &




if test -n "$somatic"; then
	echo; echo somatic
	if test -n "$single_cell"; then
		echo single_cell
		monitors.sh -nano -m12 -c6 -i$l_bed_list "$annos -gm" $ug_list $ug $recal_list &
	else
		monitors.sh -nano -m12 -c6 -i$l_bed_list "$annos -m" $ug_list $ug $recal_list &
	fi
else
	echo; echo germline
	if test -n "$single_cell"; then
		echo single_cell
		monitors.sh -nano -m12 -c6 -i$l_bed_list "$annos -g" $ug_list $ug $recal_list &
	else
		monitors.sh -nano -m12 -c6 -i$l_bed_list $annos $ug_list $ug $recal_list &
	fi
fi





monitor0707.sh -ncombh -m4 -c2 "$combine_vcfs -p$out_prefix.high" $high_list $annos $ug_list &

monitor0707.sh -ncombl -m4 -c2 "$combine_vcfs -p$out_prefix.low" $low_list $annos $ug_list &

# echo $high_list >> $hl_list
# echo $low_list >> $hl_list
# echo $out_prefix.high.combine.vcf $out_prefix.low.combine.vcf >> $combine_list
# monitor0707.sh -nimp -m4 -c2 "$impact" $combine_list $combine_vcfs $combine $hl_list &

high_list_list=$out_prefix.high.list.list.txt
low_list_list=$out_prefix.low.list.list.txt
echo $high_list >> $high_list_list
echo $low_list >> $low_list_list
monitor0707.sh -nimp -m4 -c2 "$impact" $out_prefix.high.combine.vcf $combine_vcfs $high_list_list &
monitor0707.sh -nimp -m4 -c2 "$impact" $out_prefix.low.combine.vcf $combine_vcfs $low_list_list &

#format
high_combine_list=$out_prefix.high.combine.list.txt
low_combine_list=$out_prefix.low.combine.list.txt
echo $out_prefix.high.combine.vcf >> $high_combine_list
echo $out_prefix.low.combine.vcf >> $low_combine_list

monitor0707.sh -nfmt -m10 -c1 "$format_simple -p$out_prefix.high" $out_prefix.high.combine.af.hg19_multianno.vcf $impact $high_combine_list &

monitor0707.sh -nfmt -m10 -c1 "$format_simple -p$out_prefix.low" $out_prefix.low.combine.af.hg19_multianno.vcf $impact $low_combine_list


# format_hl_list=$out_prefix.format.hl.list.txt
# echo $out_prefix.high.af.hg19_multianno.vcf >> $format_hl_list
# echo $out_prefix.low.af.hg19_multianno.vcf >> $format_hl_list
# echo snpp finished >> $end_list
# monitor0707.sh -nend -m1 -c1 "sed -i 'snpp finished'" $pid $format_simple $format_hl_list

echo snpp finished >> $log

wait

. $cmd_done